import { DataTypes } from 'sequelize';
import sequelize from '../config/db.config.js';
import Person from './Person.js';
import Common from './Common.js';

const PersonRelationship = sequelize.define('PersonRelationship', {
  personId1: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    allowNull: false,
    references: {
      model: 'persons',
      key: 'person_id'
    },
    field: 'person_id1'
  },
  personId2: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    allowNull: false,
    references: {
      model: 'persons',
      key: 'person_id'
    },
    field: 'person_id2'
  },
  relationshipTypeId: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    allowNull: false,
    field: 'relationship_type_id'
  }
}, {
  tableName: 'person_relationships',
  timestamps: true,
  paranoid: true,
  deletedAt: 'deleted_at'
});

PersonRelationship.belongsTo(Person, { as: 'RelatedPerson', foreignKey: 'personId2' });
PersonRelationship.belongsTo(Person, { as: 'Person', foreignKey: 'personId1' });
PersonRelationship.belongsTo(Common, { as: 'relationshipType', foreignKey: 'relationshipTypeId', targetKey: 'itemId' });

export default PersonRelationship; 